package Application.Components.ImageShow;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import Application.ServerHandler;
import Interfaces.IDrawableComponent;

public class ImageShowManager implements IDrawableComponent{
	private ServerHandler serverHandler;
	
	private int windowWidth, windowHeight;
	private String[] fileNames;
	
	private BufferedImage currentImage;
	private int index;
	
	public ImageShowManager(ServerHandler sh, int winWidth, int winHeight){
		this.serverHandler = sh;
		this.index = 0;
		
		this.windowWidth 	= winWidth;
		this.windowHeight	= winHeight;
	}
	
	public void nextImage(){
		fileNames = serverHandler.getImageFiles();
		
		index++;
		if(index >= fileNames.length) index = 0;
	
		try {
			currentImage = ImageIO.read(new File(ServerHandler.IMAGES_DIR + fileNames[index]));
			System.out.println("[i] Image " + fileNames[index] + " has been loaded!");
		}
		catch (IOException e) {
			System.out.println("[e] Couldn't load image: " + ServerHandler.IMAGES_DIR + fileNames[index]);
			e.printStackTrace();
		}
	}
	
	@Override
	public void draw(Graphics g) {			
		//sirsi
		if(currentImage.getWidth() > currentImage.getHeight()){
			int w = windowWidth;
			int h = (int)(currentImage.getHeight() * (windowWidth / (float)(currentImage.getWidth())));
			
			int x = 0;
			int y = (windowHeight - h) / 2;
			
			g.drawImage(currentImage, x, y, w, h, Color.WHITE, null);
		}
		//vyssi
		else{
			int w = (int)(currentImage.getWidth() * (windowHeight / (float)(currentImage.getHeight())));
			int h = windowHeight;
			
			int x = (windowWidth-w) / 2;
			int y = 0;
			
			g.drawImage(currentImage, x, y, w, h, Color.WHITE, null);
		}
	}
}
